package com.relytosoft.mizar.analyze.strategy.controller;

import com.alibaba.fastjson.JSON;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.kafka.producer.KafkaOpMsg;
import com.relytosoft.dubhe.common.kafka.producer.KafkaProducer;
import com.relytosoft.mizar.analyze.base.constant.MediaMqConstants;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.FeatureExtractionReqDto;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.TaskRequestDto;
import com.relytosoft.mizar.analyze.strategy.algorithm.structure.service.StructureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 结构化
 *
 * @Author SunLi
 * @Date: 2022/5/13 10:06
 * @Version 1.0
 */
@RestController
@RequestMapping("/strategy/structure")
@Api(tags = "结构化应用相关接口")
public class StructureController {

    @Resource
    private StructureService structureService;

    @Resource
    private KafkaProducer kafkaProducer;

    @PostMapping(value = "/add")
    @ApiOperation("实时结构化分析任务添加")
    public ResponseResult createTask(@RequestBody TaskRequestDto taskRequestDto) {
        //添加创建任务kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("insert");
        kafkaOpMsg.setData(JSON.toJSONString(taskRequestDto));
        kafkaProducer.send(MediaMqConstants.STRU_ANALYZE_TASK_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //直接调用创建任务
        //return structureService.createTask(taskRequestDto);
    }

    @GetMapping(value = "/stop")
    @ApiOperation("实时结构化分析任务删除")
    public ResponseResult stopTask(String taskId) {
        //添加创建任务kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("delete");
        kafkaOpMsg.setData(taskId);
        kafkaProducer.send(MediaMqConstants.STRU_ANALYZE_TASK_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //直接调用删除任务
        //return structureService.stopTask(taskId);
    }

    @PostMapping(value = "/fearture/extraction")
    @ApiOperation("图片分析")
    public ResponseResult feartureExtraction(@RequestBody FeatureExtractionReqDto featureExtractionReqDto) {
        return structureService.feartureExtraction(featureExtractionReqDto);
    }
}
